package frame.jserialcomm;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import com.c.rabbit_mq.ComRabbitMqHandle;
import ntd.protocol.Ntd212Parse;
import ntd.protocol.bo.Ntd212BO;
import ntd.protocol.context.Context212;

import java.util.Map;

public class SerialCommunicationDemo {
    public static void main(String[] args) {

        // 获取可用的串口列表
        ToolJSerialComm toolJSerialComm = new ToolJSerialComm("COM15", 57600, s -> {
            StaticLog.info(s);
        });

        toolJSerialComm.putMsg("KLB00010000005700131300000.00001KLE39".getBytes());

        new ComRabbitMqHandle<String>("/com-server/212/msg/41010501G00002", String.class) {
            @Override
            public void handle(String topic, String s) {
                try {
                    Context212 parse = Ntd212Parse.parse(s);
                    Ntd212BO ntd212BO = parse.getNtd212BO();
                    StaticLog.info(JSONUtil.toJsonStr(ntd212BO));
//                    toolJSerialComm.putMsg(s);
                    // 1：发送字节数-0013, 2:数据， 3：和校验
                    String strFormat = "KLB000100000057{}{}0000{}KLE";

                    Map<String, String> cpData = ntd212BO.getCPData();
                    if (MapUtil.isNotEmpty(cpData)) {
                        // 流量 - f10001
                        String f10001 = StrUtil.sub(cpData.get("f10001-Rtd"), 0, 5);
                        if (StrUtil.isNotBlank(f10001)) {
                            String length = StrUtil.padPre(StrUtil.toString(f10001.length() + 6), 4, "0");
                            putMsg(HexUtil.encodeHexStr(StrUtil.format(strFormat, length, "14", f10001)), toolJSerialComm);
                        }
                        ThreadUtil.sleep(1000);
                        // 液位 - f10011
                        String f10011 = StrUtil.sub(cpData.get("f10011-Rtd"), 0, 5);
                        if (StrUtil.isNotBlank(f10011)) {
                            String length = StrUtil.padPre(StrUtil.toString(f10011.length() + 6), 4, "0");
                            putMsg(HexUtil.encodeHexStr(StrUtil.format(strFormat, length, "15", f10011)), toolJSerialComm);
                        }
                        ThreadUtil.sleep(1000);
                        // 雨量 - f11001
                        String f11001 = StrUtil.sub(cpData.get("f11001-Rtd"), 0, 5);
                        if (StrUtil.isNotBlank(f11001)) {
                            String length = StrUtil.padPre(StrUtil.toString(f11001.length() + 6), 4, "0");
                            putMsg(HexUtil.encodeHexStr(StrUtil.format(strFormat, length, "16", f11001)), toolJSerialComm);
                        }
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }.start_rabbit_mq_listen();

    }

    private static void putMsg(String str, ToolJSerialComm toolJSerialComm) {
        String checkNum = HexUtil.encodeHexStr(String.format("%02X", checksum(HexUtil.decodeHex(str))));
        toolJSerialComm.putMsg(HexUtil.decodeHex(str + checkNum));
    }

    public static byte checksum(byte[] data) {
        int sum = 0;
        for (byte b : data) {
            sum += b & 0xFF;
        }
        return (byte)(sum & 0xFF);
    }
}